using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Tilemaps;
using UnityEngine;
namespace xingzhimeng
{
  ///<summary>
  ///
  ///</summary>
public class Pigs : MonoBehaviour
  {
      public float HP;
      public float minMagintude;//最小相对运动力
      public float[] Grading;//受伤等级
      private SpriteRenderer render;
      public Sprite[] sprite;//受伤图集
      private bool isDead;
      public GameObject Bool;//b爆炸特效
      public GameObject Score;//得分

      private void Start()
      {
          render = GetComponent<SpriteRenderer>();
      }

      private void OnCollisionEnter2D(Collision2D other)
      {
         // print(other.relativeVelocity.magnitude);
          if (other.relativeVelocity.magnitude > minMagintude)
          {
              print(other.relativeVelocity.magnitude);
              HpJian(other.relativeVelocity.magnitude);//减血
              
            
              
          }
      }
        public void HpJian(float count)
        {
            HP -= count;
            switch (SetRender())
            {
                case -1:
                    Dead();
                    break;
                case 0:
                    render.sprite = sprite[0];
                    break;
                case 1:
                    render.sprite = sprite[1];
                    break;
                case 2:
                    render.sprite = sprite[2];
                    break;
                case 3:
                    render.sprite = sprite[3];
                    break;
            }
        }
       int SetRender()
        {
            //3,7,10,14
            if (HP < 0)
                return -1;
            else if (HP < Grading[0])
                return 0;
            else if (HP < Grading[1])
                return 1;
            else if (HP < Grading[2])
                return 2;
            else if (HP < Grading[3])
                return 3;
            else return -2;

        }
      //死亡事件
      public void Dead()
      { 
         
        GameObject obj=  Instantiate(Bool, transform.position, Quaternion.identity);
        Destroy(obj,0.3f);
        GameObject score = Instantiate(Score, transform.position+new Vector3(0,.3f,0) , Quaternion.identity);
        Destroy(score,0.8f);
        GameManeger.Insnter.PigDead(this);//从集合中移除
        PlayDeadClip();//根据物体播放死亡音效
        Destroy(gameObject);


      }
       private void PlayDeadClip()
        {
            string name = "";
            switch (gameObject.tag)
            {
                case "Enemy":
                    name = ClipName.pigDead;
                    break;
                case "Plane":
                    name = ClipName.Plane;
                    break;
                case "BoLi":
                    name = ClipName.BoLi;
                    break;
            }
            Audios.Instance.PlayClip(transform.position, name);
        }
  }

}

